package com.web;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pojo.LeaveApply;
import com.pojo.OutApply;
import com.pojo.view.EmpAndLeaveApply;
import com.pojo.view.EmpAndOutApply;
import com.service.LeaveApplyService;
import com.service.OutApplyService;
import com.service.impl.LeaveApplyServiceImpl;
import com.service.impl.OutApplyServiceImpl;
import com.util.DateUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/submitServlet/*")
public class SubmitServlet extends BaseServlet {

    private LeaveApplyService leaveApplyService = new LeaveApplyServiceImpl();
    private OutApplyService outApplyService = new OutApplyServiceImpl();

    /**
     * 提交请假申请
     * API接口说明
     * POST方法 接受JSON参数
     * {"username":"zhangsan", "apply_time":"2022-05-13 15:15:45", "start_time":"2022-05-18 09:00:00", "end_time":"2022-05-19 15:00:00", "leave_category":"事假", "leave_reason":"朋友白事"}
     * 返回结果:"successful"
     */
    public void submitLeaveApply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        JSONObject jsonObject = JSON.parseObject(params);
        String username = jsonObject.get("username").toString();

        String applyTime = DateUtils.getCurrentTime();
        String startTime = jsonObject.get("start_time").toString();
        String endTime = jsonObject.get("end_time").toString();
        String leaveCategory = jsonObject.get("leave_category").toString();
        String leaveReason = jsonObject.get("leave_reason").toString();
        //status=0: "正在审核"
        Integer status = 0;

        //将对应的数据插入至数据库表tb_leaveapply中
        LeaveApply leaveApply = new LeaveApply();
        leaveApply.setApplyTime(applyTime);
        leaveApply.setStartTime(startTime);
        leaveApply.setEndTime(endTime);
        leaveApply.setLeaveCategory(leaveCategory);
        leaveApply.setLeaveReason(leaveReason);
        leaveApply.setStatus(status);
        try {
            leaveApplyService.addToLeaveApply(leaveApply);
        } catch (Exception e) {
            response.getWriter().write("add to leave apply error");
        }

        //将对应的数据插入至数据库表tb_emp_leaveapply中
        try {
            int leaveApplyId = leaveApplyService.getLeaveApplyIdByApplyTime(leaveApply.getApplyTime());
            EmpAndLeaveApply empAndLeaveApply = new EmpAndLeaveApply();
            empAndLeaveApply.setUsername(username);
            empAndLeaveApply.setLeaveApplyId(leaveApplyId);
            empAndLeaveApply.setApplyTime(applyTime);
            try {
                leaveApplyService.addToEmpLeaveApply(empAndLeaveApply);
                response.getWriter().write("successful");
            } catch (Exception e) {
                response.getWriter().write("add emp_leaveapply item error");
            }
        } catch (Exception e) {
            response.getWriter().write("get leaveapply id error");

        }
    }


    /**
     * 提交外出申请
     * API接口说明
     * POST方法 接受JSON参数
     * {"username":"zhangsan", "apply_time":"2022-05-13 15:15:45", "start_time":"2022-05-18 09:00:00", "end_time":"2022-05-19 15:00:00", "out_category":"部门事项", "out_reason":"部门外派"}
     * 返回"successful"
     */
    public void submitOutApply(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        BufferedReader bufferedReader = request.getReader();
        String params = bufferedReader.readLine();

        //设置跨域访问
        //允许跨域的主机地址
        response.setHeader("Access-Control-Allow-Origin", "*");
        //允许跨域的请求方法GET, POST, HEAD 等
        response.setHeader("Access-Control-Allow-Methods", "*");
        //重新预检验跨域的缓存时间 (s)
        response.setHeader("Access-Control-Max-Age", "4200");
        //允许跨域的请求头
        response.setHeader("Access-Control-Allow-Headers", "*");
        //是否携带cookie
        response.setHeader("Access-Control-Allow-Credentials", "true");


        JSONObject jsonObject = JSON.parseObject(params);
        String username = jsonObject.get("username").toString();

        String applyTime = DateUtils.getCurrentTime();
        String startTime = jsonObject.get("start_time").toString();
        String endTime = jsonObject.get("end_time").toString();
        String outCategory = jsonObject.get("out_category").toString();
        String outReason = jsonObject.get("out_reason").toString();
        //status=0: "正在审核"
        Integer status = 0;

        //将对应的数据插入至数据库表tb_outapply中
        OutApply outApply = new OutApply();
        outApply.setApplyTime(applyTime);
        outApply.setStartTime(startTime);
        outApply.setEndTime(endTime);
        outApply.setOutCategory(outCategory);
        outApply.setOutReason(outReason);
        outApply.setStatus(status);
        try {
            outApplyService.addToOutApply(outApply);
        } catch (Exception e) {
            response.getWriter().write("add to out apply error");
        }


        //将对应的数据插入至数据库表tb_emp_outapply中
        try {
            int outApplyId = outApplyService.getOutApplyIdByApplyTime(outApply.getApplyTime());
            EmpAndOutApply empAndOutApply = new EmpAndOutApply();
            empAndOutApply.setUsername(username);
            empAndOutApply.setOutApplyId(outApplyId);
            empAndOutApply.setApplyTime(applyTime);
            try {
                outApplyService.addToEmpOutApply(empAndOutApply);
                response.getWriter().write("successful");
            } catch (Exception e) {
                response.getWriter().write("add emp_outapply item error");
            }
        } catch (Exception e) {
            response.getWriter().write("get outapply id error");

        }

    }

}
